//
//  CSSettingTableViewControllera.m
//  classroom
//
//  Created by Macx on 16/8/4.
//  Copyright © 2016年 LiuZhuo. All rights reserved.
//

#import "CSSettingTableViewController.h"
#import "CSBuildManager.h"
#import "CSBuildPickerView.h"
#import "UMSocial.h"
#import "UMSocialData.h"
#import "UMSocialSnsService.h"
@interface CSSettingTableViewController()<UMSocialUIDelegate>
@property (nonatomic,strong) CSBuildPickerView *regionPickerView;
@property (nonatomic,strong) CSBuildPickerView *datePickerView;
@property (nonatomic,strong) UILabel *addressLabel;
@property (nonatomic,strong) UILabel *dateLabel;
@property(nonatomic,strong)LZLabel * label;
@property(nonatomic,strong)LZLabel * label2;
@end
@implementation CSSettingTableViewController
- (void)viewDidLoad{
    
    [super viewDidLoad];
    _addressLabel = [[UILabel alloc]init];
    _dateLabel = [[UILabel alloc]init];
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    _addressLabel.text = [userDefaults objectForKey:@"buildName"];
    if (_addressLabel.text == nil) {
        _addressLabel.text=@"葫芦岛校区 尔雅楼";
    }
    NSString * date =  [userDefaults objectForKey:@"dateName"];
//    _dateLabel.text = date;
    
    if (_dateLabel.text == nil) {
        _dateLabel.text =@"第1周 周一";
    }else{
        _dateLabel.text =[[[CSGetTrueDay alloc]init] dateName:date];
    }
    [self setupGroup0];
    [self setupGroup01];
    [self setupGroup1];
    [self setupGroup2];
    [self setupGroup3];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
 *  0组
 */
- (void)setupGroup0
{
    LZLabel *theme = [LZLabel itemWithTitle:@"默认教学楼"];
    
    _label=theme;
    theme.label=_addressLabel.text;
    LZSection *group0 = [[LZSection alloc]init];
    group0.headerLabel = @"更改默认教学楼";
    group0.items = @[theme];
    [self.dataArray addObject:group0];
    
}
-(void)setupGroup01{
    LZLabel *theme = [LZLabel itemWithTitle:@"当前日期"];
    
    _label2=theme;
    theme.label=_dateLabel.text;
    LZSection *group0 = [[LZSection alloc]init];
    group0.headerLabel = @"首次使用或每个学期初需要自己手动设置";
    group0.items = @[theme];
    [self.dataArray addObject:group0];
}

- (void)setupGroup1
{
    
    LZSwitch *wifi = [LZSwitch itemWithTitle:@"仅Wifi状态下连网"];
    
    LZSection *group1 = [[LZSection alloc]init];
    group1.headerLabel = @"开启后，将仅在Wifi状态下加载数据";
    group1.items = @[wifi];
    [self.dataArray addObject:group1];
}
-(void)setupGroup2{
    LZSwitch *noti = [LZSwitch itemWithTitle:@"推送"];
    noti.ison = YES;
    LZSection *group2 = [[LZSection alloc]init];
    group2.headerLabel = @"开启后，将在每天8点推送当天默认教学楼教室占用情况";
    group2.items = @[noti];
    [self.dataArray addObject:group2];
    
}
- (void)setupGroup3
{
    
    LZAccess *safe = [LZAccess itemWithTitle:@"检验更新"];

    __weak typeof(self) weakSelf = self;
//    __weak typeof(safe) weakSafe = safe;
    safe.block = ^{
        // 循环引用
//        weakSafe.deLabel = nil;
//        [weakSelf.tableView reloadData];
        [ SVProgressHUD showWithStatus:@"检验中"];
        
        [self performSelector:@selector(miss) withObject:nil afterDelay:1];
      
       
        
    };
      LZAccess *share = [LZAccess itemWithTitle:@"分享给好友"];
    
    share.block=^{
        [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:@"http://www.baidu.com/img/bdlogo.gif"];
        [UMSocialData defaultData].extConfig.title = @"教室查询系统";
        [UMSocialData defaultData].extConfig.qqData.url = @"http://www.lgdjwc.cn";
        [UMSocialData defaultData].extConfig.sinaData.urlResource =[[UMSocialUrlResource alloc]initWithSnsResourceType:UMSocialUrlResourceTypeDefault url:@"http://www.lgdjwc.cn"];
        [UMSocialData defaultData].extConfig.wechatFavoriteData.url = @"http://www.lgdjwc.cn";
        [UMSocialData defaultData].extConfig.wechatSessionData.url = @"http://www.lgdjwc.cn";
        [UMSocialData defaultData].extConfig.wechatTimelineData.url = @"http://www.lgdjwc.cn";
        [UMSocialData defaultData].extConfig.qzoneData.url = @"http://www.lgdjwc.cn";
        [UMSocialSnsService presentSnsIconSheetView:self
                                             appKey:@"57b1c38167e58ebbf40033f9"
                                          shareText:@"我正在使用辽工大教室查询系统,界面单纯不做作,跟外面那群妖艳的贱货完全不一样"
                                         shareImage:[UIImage imageNamed:@"AppIcon"]
                                    shareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSina,UMShareToQQ,UMShareToQzone]
                                           delegate:weakSelf];
    };
    LZAccess *about = [LZAccess itemWithTitle:@"关于"];
    about.nibName=@"CSAboutTableViewController";
    LZSection *group3 = [[LZSection alloc]init];
    group3.items = @[safe,share,  about];
    [self.dataArray addObject:group3];
}
-(void)diss{
    [SVProgressHUD dismiss];
}
-(void)miss{
    [SVProgressHUD showErrorWithStatus:@"当前为最新版本"];
    [self performSelector:@selector(diss) withObject:nil afterDelay:1];
}
- (void)dealloc
{
    DDLogDebug(@"%s",__func__);
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LZSection * sec = self.dataArray[indexPath.section];
    LZItem * it = sec.items[indexPath.row];
    if (indexPath.section == 0) {
        //        self.tabBarController.hidesBottomBarWhenPushed = YES;
        NSString *address = _addressLabel.text;
        NSArray *array = [address componentsSeparatedByString:@" "];
        
        NSString *area = @"";//校区
        NSString *build = @"";//教学楼
        if (array.count > 1) {
            area = array[0];
            build = array[1];
        } else if (array.count > 0) {
            build = array[0];
        }
        
        [self.regionPickerView showPickerWithAreaName:area buildName:build];
        
        
    }else if(indexPath.section == 1){
        NSString *date = _dateLabel.text;
        NSArray *array = [date componentsSeparatedByString:@" "];
        
        NSString *week = @"";//第几周
        NSString *day = @"";//周几
        if (array.count > 1) {
            week = array[0];
            day = array[1];
        } else if (array.count > 0) {
            day = array[0];
        }
        
        [self.datePickerView showPickerWithWeekName:week dayName:day];
    }else if ([it isKindOfClass:[LZAccess class]]){
        
        LZAccess * item = (LZAccess *) it;
        if (item.nibName) {
            UIStoryboard * sb = [UIStoryboard storyboardWithName:item.nibName bundle:nil];
            UIViewController * vc = [sb instantiateInitialViewController];
            vc.title = item.title;
            [self.navigationController pushViewController:vc animated:YES];
        }else if (it.block!=nil) {
            it.block();
        }
    }
    
}


- (CSBuildPickerView *)regionPickerView {
    if (!_regionPickerView) {
        _regionPickerView = [[CSBuildPickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        
        __weak typeof(self) wself = self;
        
        
        _regionPickerView.completion = ^(NSString *areaName,NSString *buildName) {
            __strong typeof(wself) self = wself;
            self.addressLabel.text = [NSString stringWithFormat:@"%@ %@",areaName,buildName];
            self.label.label = self.addressLabel.text;
            if ([self.label.label hasSuffix:@"楼"]) {
                NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
                [userDefaults setObject:self.label.label  forKey:@"buildName"];
                [userDefaults synchronize];
                [ self.tableView reloadData];
            }
            
            
        };
        
        
        [self.tabBarController.view addSubview:_regionPickerView];
    }
    
    return _regionPickerView;
}
-(CSBuildPickerView *)datePickerView{
    if (!_datePickerView) {
        _datePickerView = [[CSBuildPickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        
        __weak typeof(self) wself = self;
        
        
        _datePickerView.dateCompletion= ^(NSString *weekName,NSString *dayName) {
            __strong typeof(wself) self = wself;
            self.dateLabel.text = [NSString stringWithFormat:@"%@ %@",weekName,dayName];
            self.label2.label = self.dateLabel.text;
            if (self.label2.label.length>4) {
                NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
                NSDate * date = [[NSDate alloc]init];
                [userDefaults setObject:date  forKey:@"date"];
                [userDefaults setObject:self.label2.label  forKey:@"dateName"];
                [userDefaults synchronize];
                [ self.tableView reloadData];
            }
        };
        
        [self.tabBarController.view addSubview:_datePickerView];
    }
    
    return _datePickerView;
    
}


@end
